﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web.Mvc;
using MvcEngine.Core.Services;
using MvcEngine.Mvc.Constraints;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Mvc.Filters;
using MvcEngine.Core;
using MvcEngine.Mvc.StarterSite.Controllers;
using MvcEngine.Core.Content;
using MvcEngine.Mvc;
using MvcEngine.Core.Registry;
using MvcEngine.HtmlText.ModelBinders;
using MvcEngine.HtmlText.Models.Entities;
using MvcEngine.Blog.Components.Services;
using MvcEngine.HtmlText.Repositories;
using MvcEngine.HtmlText.StarterSite.Controllers;

namespace MvcEngine.HtmlText
{
    public class HtmlTextControlService : IControlService
    {
        public void RegisterBackofficeRoutes(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
                "BackOffice_HtmlTextList",
                "BackOffice/Texts",
                new { controller = "Text", action = "List" },
                new string[] { "MvcEngine.HtmlText.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_HtmlTextPaging",
                "BackOffice/Texts/Page{pageNumber}",
                new { controller = "Text", action = "List", pageNumber = "0" },
                new string[] { "MvcEngine.HtmlText.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_HtmlTextEdit",
                "BackOffice/Text/Edit/{ctrId}",
                new { controller = "Text", action = "Edit" },
                new string[] { "MvcEngine.HtmlText.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_HtmlTextSave",
                "BackOffice/Text/Save/{ctrId}",
                new { controller = "Text", action = "Save" },
                new string[] { "MvcEngine.HtmlText.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_HtmlTextUploadFile",
                "BackOffice/Text/UploadFile/{ctrId}",
                new { controller = "Text", action = "UploadFile" },
                new string[] { "MvcEngine.HtmlText.BackOffice.Controllers" }
            );
        }

        public void RegisterStarterSiteRoutes(AreaRegistrationContext context)
        {
            context.MapModuleRoute(
                "HtmlText",
                "{pageName}/Text/{ctrId}",
                new { actionName = "Index" },
                new string[] { "MvcEngine.HtmlText.StarterSite.Controllers" }
            );

            context.MapImageRoute(
                "HtmlTextFile",
                "HtmlText/{ctrId}/File/{fileName}",
                new HtmlTextFileResolver(),
                new string[] { "MvcEngine.HtmlText.StarterSite.Controllers" }
            );
        }

        public void DeleteControlFromPage(int pageContentId, Guid controlId)
        {
#warning delete image files
            IoC.Resolve<IHtmlTextRepository>().Delete(pageContentId);
        }

        public IList<ControlCommand> GetCommands()
        {
            List<ControlCommand> commands = new List<ControlCommand>();
            commands.Add(new EditCommand() { Name = "Edit" });
            return commands;
        }

        public IEnumerable<string> GetStyleSheets()
        {
            return Enumerable.Empty<string>();
        }

        public void RegisterActionFilters(IActionFilterRegistry registry)
        {
            ControllerCriteria contentCriteria = new ControllerCriteria();
            contentCriteria.AddController(typeof(MvcEngine.HtmlText.BackOffice.Controllers.TextController));
            registry.Add(new[] { contentCriteria }, typeof(ModuleActionFilter));

            //--------------------------------------------------------------------------------------
            //---------------------------------Permissions------------------------------------------
            //--------------------------------------------------------------------------------------
            ControllerCriteria viewCriteria = new ControllerCriteria();
            viewCriteria.AddController(typeof(MvcEngine.HtmlText.StarterSite.Controllers.TextController));
            ControllerCriteria editCriteria = new ControllerCriteria();
            editCriteria.AddController(typeof(MvcEngine.HtmlText.BackOffice.Controllers.TextController));

            registry.Add(new[] { editCriteria }, new RolePermissionAttribute(Constants.Roles.Administrators));
            registry.Add(new[] { viewCriteria }, new ModulePermissionAttribute(PermissionTypes.View));
            //--------------------------------------------------------------------------------------
            //---------------------------------End of permissions----------------------------------
            //--------------------------------------------------------------------------------------
        }

        public void RegisterModelBinders(ModelBinderDictionary binders)
        {
            binders[typeof(HtmlTextItem)] = new HtmlTextModelBinder();
        }

        public Type GetControllerType()
        {
            return typeof(TextController);
        }

        public string GetDefaultAction()
        {
            return "Index";
        }
    }
}
